<%@ page contentType="text/html;charset=UTF-8" %>
<%@ include file="/WEB-INF/views/include/taglib.jsp" %>
<html>
<head>
    <title>图书管理</title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <meta name="decorator" content="default"/>
    <script type="text/javascript">
        document.onkeydown = function (event) {
            var e = event || window.event || arguments.callee.caller.arguments[0];
            if(e && e.keyCode == 9){
                if (e.preventDefault) {
                    e.preventDefault();
                }
                else {
                    e.returnValue = false;
                }
                var input_name = document.activeElement.name;
                var flag = false;
                $(".tab_b").each(function () {
                    if (flag) {
                        $(this).focus();
                        return false;
                    } else {
                        flag = ($(this).attr("name") == input_name);
                    }
                });
            } else if (e.altKey && e.keyCode == 83) {
                submitBookInfo();
            } else if (e.altKey && e.keyCode == 66) {
                history.go(-1);
            }
        };

        jQuery.extend(jQuery.validator.messages, {
            required: "必填"
        });

        $(document).ready(function() {
            isbnFocus();

            if ("${message}" != "" && "${message}" != "null") {
                showTip("${message}");
            }
            $("#bkInfoForm").validate({
                submitHandler: function (form) {
                    loading('正在提交，请稍等...');
                    form.submit();
                },
                errorPlacement: function(error, element) {
                    if (element.parent().is(".input-append")) {
                        error.innerHTML = "必填";
                        error.innerText = "必填";
                        error.appendTo(element.parent().parent());
                    } else {
                        error.insertAfter(element);
                    }
                }
            });
        });

        function submitBookInfo(){
            $("#bkInfoForm").submit();
        }

        function searchBookEnterPress(e) { //传入 event
            var e = event || window.event || arguments.callee.caller.arguments[0];
            if (e && e.keyCode == 13) {
                searchBookFromApi();
            }
        }

        function setupPopUp(){
            $(".nav-tabs").hide();
            $("#goback").hide();
            var bkInfoForm = $("#bkInfoForm");
            var src = bkInfoForm.attr("action");
            if (src.indexOf("?") >= 0) {
                src = src + "&pop=true"
            }else{
                src = src + "?pop=true"
            }
            bkInfoForm.attr("action", src)
        }

        function popBooksSelect(){
            // 正常打开
            var url = encodeURI("${ctx}/book/books/listData?booksName=" + $("#booksName").val());
            top.$.jBox.open("iframe:" + url, "选择从编", 550, 420, {
                buttons: {"确定": "ok", "关闭": true}, submit: function (v, h, f) {
                    if (v == "ok") {
                        var value = h.find("iframe")[0].contentWindow.getSelectValue();
                        $("#booksName").val(value);
                    }
                }, loaded: function (h) {
                    $(".jbox-content", top.document).css("overflow-y", "hidden");
                }, closed: function () {
                    $("#booksName").val(window.parent.books);
                }
            });
        }
        function popPublisherSelect(){
            // 正常打开
            var url = encodeURI("${ctx}/book/publisher/listData?publisher=" + $("#publisher").val());
            top.$.jBox.open("iframe:" + url, "选择出版社", 550, 420, {
                buttons: {"确定": "ok", "关闭": true}, submit: function (v, h, f) {
                    if (v == "ok") {
                        var value = h.find("iframe")[0].contentWindow.getSelectValue();
                        $("#publisher").val(value);
                    }
                }, loaded: function (h) {
                    $(".jbox-content", top.document).css("overflow-y", "hidden");
                }, closed: function () {
                    $("#publisher").val(window.parent.publisher);
                    $("#pubplace").val(window.parent.prAddress);
                }
            });
        }
        function selectPublisher(){
            $.ajax({
                type: "POST",
                url: "${ctx}/book/publisher/findAddress",
                data: {prName: $("#publisher").val()},
                dataType: "json",
                success: function (data) {
                    $("#pubplace").val(data.prAddress)
                }
            });
        }

        function searchBookFromApi(){
            var isbn = $("#bookIsbn").val();
            $.ajax({
                type: "POST",
                url: "${ctx}/book/manage/searchBooksFromApi",
                data: {isbn: isbn},
                dataType: "json",
                success: function (data) {
                    if("0001"==data.code) {
                        var bookinfo = data.data;
                        $("input[name='bookname']").val(bookinfo.bookname);
                        $("input[name='pinyin']").val(bookinfo.pinyin);
                        $("input[name='othername']").val(bookinfo.othername);
                        $("input[name='author']").val(bookinfo.author);
                        $("input[name='edition']").val(bookinfo.edition);
                        $("input[name='pubplace']").val(bookinfo.pubplace);
                        $("input[name='publisher']").val(bookinfo.publisher);
                        $("input[name='pubdate']").val(bookinfo.pubdate);
                        $("input[name='pages']").val(bookinfo.pages);
                        $("input[name='appendix']").val(bookinfo.appendix);
                        $("input[name='classcode']").val(bookinfo.classcode);
                        $("input[name='className']").val(bookinfo.className);
                        $("input[name='version']").val(bookinfo.version);
                        $("input[name='price']").val(bookinfo.price);
                        $("input[name='books']").val(bookinfo.books);
                        $("input[name='booksNa']").val(bookinfo.booksNa);
                        $("input[name='booksNum']").val(bookinfo.booksNum);
                        $("input[name='booksauthor']").val(bookinfo.booksauthor);
                        $("input[name='endnote']").val(bookinfo.endnote);
                        $("input[name='docclass']").val(bookinfo.docclass);
                        $("input[name='measure']").val(bookinfo.measure);
                        $("input[name='ztc']").val(bookinfo.ztc);
                        $("input[name='lagclass']").val(bookinfo.lagclass);
                        $("input[name='marc']").val(bookinfo.marc);
                        $("input[name='cbbh']").val(bookinfo.cbbh);
                        $("input[name='tubiao']").val(bookinfo.tubiao);
                        $("input[name='issn']").val(bookinfo.issn);
                        $("input[name='cnissn']").val(bookinfo.cnissn);
                        $("input[name='zdcode']").val(bookinfo.zdcode);
                        $("#remarks").html(bookinfo.remarks);
                        $("input[name='theme']").val(bookinfo.theme);
                        $("#classcode").focus();
                    }else{
                        alertx("没有查询到图书信息！", isbnFocus);
                        $(".jbox-button-focus").focus();
                    }
                }
            });
        }
        function isbnFocus(){
            $("#bookIsbn").focus();
        }
        $.fn.serializeObject = function()
        {
            var o = {};
            var a = this.serializeArray();
            $.each(a, function() {
                if (o[this.name]) {
                    if (!o[this.name].push) {
                        o[this.name] = [o[this.name]];
                    }
                    o[this.name].push(this.value || '');
                } else {
                    o[this.name] = this.value || '';
                }
            });
            return o;
        };
    </script>
    <style>
        .form-horizontal .control-label{
            width: 70px;
        }

        .form-horizontal .controls {
            margin-left: 80px;
            overflow-x: hidden;
        }

        .half {
            width: 48%;
            float: left;
        }
        .thri {
            width: 32%;
            float: left;
        }
        .all {
            width: 96%;
            float: left;
        }
        .half .input_ln{width: 220px!important;}
        .all .input_ln{width: 220px!important;}
        .input_ln{min-width:150px;width: 80%;max-width: 220px;}
        .input-append input{width: 165px;}
        .form-horizontal{
            min-width: 1300px;
        }
    </style>
</head>
<body>
<ul class="nav nav-tabs">
    <li>
        <a href="${ctx}/book/manage/list">书籍列表</a>
    </li>
    <li>
        <a href="${ctx}/book/manage/form">导入marc</a>
    </li>
    <li class="active">
        <c:if test="${empty bkBookinfo.id}">
            <a href="${ctx}/book/manage/get">新增书目</a>
        </c:if>
        <c:if test="${not empty bkBookinfo.id}">
            <a href="${ctx}/book/manage/get?bkId=${bkBookinfo.id}">修改书目</a>
        </c:if>
    </li>
</ul>
<br/>
<form:form id="bkInfoForm" modelAttribute="bkBookinfo" action="${ctx}/book/manage/edit" method="post" class="form-horizontal">
    <form:hidden id="bkId" path="id"/>
    <div class="control-group all">
        <label class="control-label">ISBN：</label>

        <div class="controls">
            <c:if test="${not empty bkBookinfo.id}">
                <label class="lbl">${bkBookinfo.isbn}</label>
            </c:if>
            <c:if test="${empty bkBookinfo.id}">
                <form:input id="bookIsbn" path="isbn" cssClass="required valid input_ln tab_b" onkeypress="searchBookEnterPress(event)" />
                <input class="btn btn-primary" onclick="searchBookFromApi()" type="button" value="联网查询"/>
            </c:if>
            <span class="help-inline"><font color="red">*</font> </span>
        </div>
    </div>
    <div class="control-group thri">
        <label class="control-label">正题名：</label>

        <div class="controls">
            <form:input path="bookname" cssClass="required valid input_ln tab_b"/>
            <span class="help-inline"><font color="red">*</font> </span>
        </div>
    </div>
    <div class="control-group thri">
        <label class="control-label">副题名：</label>

        <div class="controls">
            <form:input path="othername" cssClass="input_ln tab_b"/>
        </div>
    </div>
    <div class="control-group thri">
        <label class="control-label">拼音标题：</label>

        <div class="controls">
            <form:input path="pinyin" cssClass="input_ln tab_b"/>
        </div>
    </div>
    <div class="control-group thri">
        <label class="control-label">编著者：</label>

        <div class="controls">
            <form:input path="author" cssClass="required valid input_ln tab_b"/>
            <span class="help-inline"><font color="red">*</font> </span>
        </div>
    </div>
    <div class="control-group thri">
        <label class="control-label">分类号：</label>

        <div class="controls">
            <form:input id="classcode" path="classcode" cssClass="required valid input_ln tab_b"/>
            <span class="help-inline"><font color="red">*</font></span>
        </div>
    </div>
    <div class="control-group thri">
        <label class="control-label">版次：</label>

        <div class="controls">
            <form:input path="version" cssClass="digits input_ln tab_b"/>
            <span class="help-inline"></span>
        </div>
    </div>
    <div class="control-group thri">
        <label class="control-label">丛编：</label>

        <div class="controls">
            <div class="input-append">
                <input id="booksId" name="booksId" type="hidden"/>
                <form:input id="booksName" path="books" cssClass="input_ln tab_b"/>
                <a href="javascript:void(0)" onclick="popBooksSelect()" class="btn">&nbsp;<i class="icon-search"></i>&nbsp;</a>&nbsp;&nbsp;
            </div>
        </div>
    </div>
    <div class="control-group half">
        <label class="control-label">分册号：</label>

        <div class="controls">
            <form:input id="booksNum" path="booksNum" cssClass="input_ln tab_b"/>
        </div>
    </div>

    <div class="control-group thri">
        <label class="control-label">出版社：</label>

        <div class="controls">
            <div class="input-append">
                <form:input id="publisher" path="publisher" cssClass="required input_ln tab_b"/>
                <a href="javascript:void(0)" onclick="popPublisherSelect()" class="btn">&nbsp;<i class="icon-search"></i>&nbsp;</a>&nbsp;&nbsp;
            </div>
            <span class="help-inline"><font color="red">*</font> </span>
        </div>
    </div>
    <div class="control-group thri">
        <label class="control-label">出版地：</label>

        <div class="controls">
            <form:input id="pubplace" path="pubplace" cssClass="input_ln tab_b"/>
        </div>
    </div>
    <div class="control-group thri">
        <label class="control-label">出版日期：</label>

        <div class="controls">
            <form:input path="pubdate" htmlEscape="false" class="input_ln tab_b"/>
        </div>
    </div>

    <div class="control-group all">
        <label class="control-label">版次标识：</label>

        <div class="controls">
            <form:input path="edition" cssClass="input_ln tab_b"/>
        </div>
    </div>

    <div class="control-group thri">
        <label class="control-label">页码：</label>

        <div class="controls">
            <form:input path="pages" cssClass="input_ln tab_b"/>
        </div>
    </div>

    <div class="control-group thri">
        <label class="control-label">开本：</label>

        <div class="controls">
            <form:select path="measure" items="${fns:getDictList('measure')}" itemLabel="label" itemValue="value" class="input-medium" />
        </div>
    </div>

    <div class="control-group thri">
        <label class="control-label">价格：</label>

        <div class="controls">
            <form:input path="price" htmlEscape="false" cssClass="required number input_ln tab_b"/>
            <span class="help-inline"><font color="red">*</font></span>
        </div>
    </div>

    <div class="control-group half">
        <label class="control-label">使用对象：</label>

        <div class="controls">
            <form:input path="user" cssClass="input_ln tab_b"/>
        </div>
    </div>

    <div class="control-group all">
        <label class="control-label">主题：</label>

        <div class="controls">
            <form:input path="theme" cssClass="input_ln tab_b"/>
            <span class="help-inline">多个主题请使用半角逗号（,）号隔开</span>
        </div>
    </div>

    <div class="control-group all">
        <label class="control-label">摘要：</label>

        <div class="controls">
            <form:textarea path="remarks" cssStyle="width: 60%" cssClass=" tab_b"/>
        </div>
    </div>
    <div id="btn_group" class="control-group all">
        <shiro:hasPermission name="book:manage:save">
        <input class="btn btn-primary tab_b" style="width:20%;margin-left: 1%;" type="button" onclick="submitBookInfo()" value="保存 Alt+S"/>
        </shiro:hasPermission>
        <input id="goback" class="btn btn-primary" style="width:20%;margin-left: 1%;" onclick="history.go(-1);" type="button" value="返回 Alt+B"/>
    </div>
</form:form>
</body>
</html>